package com.example.demo.common;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class NewResult<T> {
    private Integer ErrorCode;
    private String Msg;
    private Boolean Success;
    private T Data;

    public Integer getErrorCode() {
        return ErrorCode;
    }

    public void setErrorCode(Integer code) {
        this.ErrorCode = code;
    }

    public String getMsg() {
        return Msg;
    }

    public void setMsg(String msg) {
        this.Msg = msg;
    }

    public Boolean getSuccess() {
        return Success;
    }

    public void setSuccess(Boolean success) {
        this.Success = success;
    }

    public T getData() {
        return Data;
    }

    public void setData(T data) {
        this.Data = data;
    }


    public NewResult() {
    }

    public NewResult(T data) {
        this.Data = data;
    }

    public static NewResult success() {
        NewResult newResult = new NewResult<>();
//        result.setCode("0");
        newResult.setSuccess(true);
        newResult.setErrorCode(0);
        newResult.setMsg("请求成功!");
        return newResult;
    }

    public static <T> String success(T data) {
        NewResult<T> newResult = new NewResult<>(data);
        newResult.setSuccess(true);
        newResult.setErrorCode(0);
        newResult.setMsg("请求成功!");
        JSONObject json = JSONUtil.parseObj(newResult, false);
//        String jsonstring = JSON.toJSONString(result);
//        return "ppp("+json.toStringPretty()+")";
        return json.toStringPretty();
    }

    public static NewResult error(Integer code, String msg) {
        NewResult newResult = new NewResult();
        newResult.setSuccess(false);
        newResult.setErrorCode(code);
        newResult.setMsg(msg);
        return newResult;
    }
}
